<template>
  <div ref="qualification" class="_wz_cent_main _wz_qualification">
    <Card :bordered="true" style="margin:0 0 10px;" id="_wz_top_search_qualification">
      <p slot="title">安全资质证明</p>
      <Form ref="formSearch" :model="formDataSearch" label-position="right" :label-width="120" inline>
        <FormItem label="运输企业">
          <i-select v-model="formDataSearch.deptUuid" filterable placeholder="请选择运输企业" clearable>
            <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{ item.deptName }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="选择人员">
          <i-select v-model="formDataSearch.driverUuid" filterable placeholder="请选择选择人员" clearable>
            <Option v-for="item in trailerList" :value="item.uuid" :key="item.uuid">{{ item.driverName }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="年份选择">
          <DatePicker v-model="formDataSearch.year" type="year" placeholder="请选择年份"></DatePicker>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="searchSubmit">搜索</Button>
        </FormItem>
      </Form>
    </Card>
    <Card :bordered="true">
      <p slot="title">列表</p>
      <p slot="extra">
        <Button type="primary" @click="addTableData" v-ButtonPerm="'safety:certificate:save'">添加</Button>
      </p>
      <div>
        <Table stripe highlight-row ref="table" :loading="tableLoading" border :columns="columns2" :data="tableData" :height="tableHeight"></Table>
      </div>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page ref="tablePage" :total="tableTotal" show-sizer :current="page" show-elevator show-total @on-change="changePage" @on-page-size-change="changePageSize" />
        </div>
      </div>
    </Card>
    <Drawer :title="formTitle" v-model="formOperation" width="800" :mask-closable="false" :draggable="true" :styles="formOperationStyles">
      <QualificationAdd v-if="formOperation" @closeFun="closeFun" :selectdata="selectdata"></QualificationAdd>
    </Drawer>


    <Drawer title="查看详情" v-model="seeMoreType" width="800" :mask-closable="false" :draggable="true">
      <QualificationData v-if="seeMoreType" :selectdata="selectdata"></QualificationData>
    </Drawer>


  </div>
</template>
<script>


  import QualificationAdd from "../../components/safety/qualification/QualificationAdd";
  import QualificationData from "../../components/safety/qualification/QualificationData";

  export default {
    components: {
      QualificationAdd,//添加
      QualificationData,//查看详情
    },
    data() {
      return {
        seeMoreType: false,
        formDataSearch: {},//搜索表单数据
        searchCriteria: {},//点击搜索 从新赋值
        formTitle: '',
        selectdata: null,
        formOperation: false,//表单左侧移出
        formOperationStyles: {//表单左侧移出css
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          padding: '10px 10px 53px 10px',
          position: 'static'
        },
        tableHeight: 450,//表格默认高度
        tableLoading: true,//表格加载
        columns2: [//表格表头数据
          {type: 'index', width: 70, align: 'center', title: '序号'},
          {title: '人员名称', minWidth: 120, key: 'driverName'},
          {title: '人员类型', minWidth: 180, key: 'driverTypeName'},
          {title: '联系方式', minWidth: 200, key: 'driverPhone'},
          {title: '身份证号', minWidth: 200, key: 'driverIdentifyNum'},
          {title: '安全资质证有效期', minWidth: 160, key: 'driverValidDate'},
          {
            title: '操作', fixed: 'right', width: 220,
            render: (h, params) => {
              var operationCertificate = [];
              //修改
              if (JSON.parse(sessionStorage.getItem('loginData')).authority.SAFETY.includes("safety:certificate:update")) {
                operationCertificate.push(
                  h('Button', {
                    props: {size: 'small', type: 'warning',},
                    style: {"margin": "0px 5px 0 0"},
                    on: {
                      click: () => {
                        this.editTableData(params.row);
                      }
                    }
                  }, '修改')
                );
              }
              //删除
              if (JSON.parse(sessionStorage.getItem('loginData')).authority.SAFETY.includes("safety:certificate:delete")) {
                operationCertificate.push(
                  h('Button', {
                    props: {size: "small", type: "error"},
                    style: {"margin": "0px 5px 0 0"},
                    on: {
                      click: () => {
                        this.removeTableFun(params.row)
                      }
                    }
                  }, "删除")
                );
              }
              operationCertificate.push(
                h('Button', {
                  props: {size: "small", type: "info"},
                  style: {"margin": "0px 5px 0 0"},
                  on: {
                    click: () => {
                      this.seeMore(params.row);
                    }
                  }
                }, "详情")
              )
              return h('div', operationCertificate);
            }
          },
        ],
        page: 1,//当前页码
        limit: 10,//当前页码
        tableTotal: 0,//总条数
        tableData: [],//表格数据

        trailerList: [],
        subDeptList: [],//运输企业
      }
    },
    beforeCreate() {//beforeCreate创建前状态

    },
    created() {//created创建完毕状态
    },
    beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

    },
    mounted() {//mounted 挂载结束状态
      var that = this;
      that.tableLoadData();
      that.$nextTick(function () {
        var tabHeight = 0;
        if (!!localStorage.getItem('systemStyle')) {
          var systemStyle = JSON.parse(localStorage.getItem('systemStyle'));
          tabHeight = !!systemStyle.multiTabbed ? 32 : 0;
        }
        that.tableHeight = window.innerHeight - document.getElementById("_wz_top_search_qualification").offsetHeight - 10 - 51 - 32 - 32 - 64 - 31 - 34 - 10 - tabHeight;
        window.onresize = function () {
          that.tableHeight = window.innerHeight - document.getElementById("_wz_top_search_qualification").offsetHeight - 10 - 51 - 32 - 34 - 64 - 31 - 34 - 10 - tabHeight;
        };
      })


      //人员数据
      that.axios.get(that.apiUrl.basicDriverSafetyList)
      .then(res => {
        if (!!res) {
          that.trailerList = res.data.data;
        }
      }).catch(err => {
        console.log("失败", err)
      })

      //运输企业
      that.axios.get(that.apiUrl.subDeptList)
      .then(res => {
        if (!!res) {
          that.subDeptList = res.data.list;
        }
      }).catch(err => {
        console.log("失败", err)
      })

    },
    methods: {//执行的方法
      tableLoadData() {//加载表格数据
        var that = this;
        that.tableLoading = true;//加载
        var searchCriteria = that.searchCriteria;//获取表单数据  和分页 条数
        searchCriteria.page = that.page;
        searchCriteria.limit = that.limit;
        if (!!searchCriteria.year) {
          searchCriteria.year = that.public.timeFormatting(searchCriteria.year, "yyyy");
        }
        that.axios.get(that.apiUrl.safetyCertificatePageList, {params: searchCriteria})
        .then(res => {
          if (!!res) {
            that.tableLoading = false;
            that.tableTotal = res.data.count;
            that.tableData = res.data.data;//返回数据
          }
        }).catch(err => {
          console.log("失败", err)
        })
      },
      changePage(page) {//返回第几页
        this.page = page;//设置当前页数
        this.tableLoadData();
      },
      changePageSize(size) {
        this.limit = size;//设置当前页数
        this.tableLoadData();
      },
      addTableData() {//table添加
        var that = this;
        that.formTitle = "安全资质证明添加";
        that.selectdata = null;
        that.formOperation = true;
      },
      editTableData(data) {//table修改
        var that = this;
        that.formTitle = "安全资质证明修改";
        that.selectdata = data;
        that.formOperation = true;
      },
      removeTableFun(data) {//删除
        var that = this;
        that.$Modal.confirm({
          title: '删除提醒',
          content: '确定要删除选中的记录？',
          onOk: () => {
            that.$Spin.show();
            that.axios.post(that.apiUrl.safetyCertificateDelete + data.id)
            .then(res => {
              that.$Spin.hide();
              if (!!res) {
                that.$Message.success('数据删除成功.');
                that.tableLoadData();
              }
            }).catch(err => {
              console.log("失败", err)
            })
          },
          onCancel: () => {
            //点击取消
          }
        });
      },
      //查看详情
      seeTableFun(data) {
        var that = this;
        that.formDetailsData = data;
        that.formDetails = true;
      },
      searchSubmit() {//搜索
        this.page = 1;//设置当前页数
        this.searchCriteria = JSON.parse(JSON.stringify(this.formDataSearch));
        this.tableLoadData();
      },
      //添加修改触发后关闭刷新
      closeFun(data) {
        var that = this;
        if (data) {//添加修改 刷新表单
          that.formOperation = false;
          that.tableLoadData();
        } else {//取消关闭  不刷新
          that.formOperation = false;
        }
      },
      seeMore(data) {
        var that = this;
        that.selectdata = data;
        that.seeMoreType = true;
      },
    },
    watch: {//监听

    },
    props: {//接收来自父组件的数据

    },
    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>
<style lang="less">
  ._wz_qualification {

  }
</style>
